//
//  NSString+Add.m
//  AtCar
//
//  Created by 代佳宏 on 2020/11/25.
//  Copyright © 2020 车一转. All rights reserved.
//

#import "NSString+Add.h"

@implementation NSString (Add)

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    
    if (!font) {
        font = [UIFont systemFontOfSize:12];
    }

    NSMutableDictionary *attr = [NSMutableDictionary new];
    attr[NSFontAttributeName] = font;
    if (lineBreakMode != NSLineBreakByWordWrapping) {
        NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
        paragraphStyle.lineBreakMode = lineBreakMode;
        attr[NSParagraphStyleAttributeName] = paragraphStyle;
    }
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:attr context:nil];
    result = rect.size;
    
    return result;
}

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

- (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width {
    CGSize size = [self sizeForFont:font size:CGSizeMake(width, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}

/// 把分转成元字符串
/// @param cents 分
+ (NSString *)changeMoneyFromCents:(NSInteger)cents {
    /// 是否为负数
    BOOL isNegative = NO;
    if (cents < 0) {
        cents *= - 1;
        isNegative = YES;
    }
    NSInteger beforeDot = cents / 100;
    NSInteger afterDot = cents % 100;
    if (afterDot == 0) {
        return [NSString stringWithFormat:@"%@%@",(isNegative ? @"-" : @""),@(beforeDot)];
    }
    else if (afterDot < 10) {
        return [NSString stringWithFormat:@"%@%@.0%@",(isNegative ? @"-" : @""),@(beforeDot),@(afterDot)];
    }
    else if ((afterDot % 10) == 0) {
        return [NSString stringWithFormat:@"%@%@.%@",(isNegative ? @"-" : @""),@(beforeDot),@(afterDot / 10)];
    }
    else {
        return [NSString stringWithFormat:@"%@%@.%@",(isNegative ? @"-" : @""),@(beforeDot),@(afterDot)];
    }
}

/// 把分转成折扣字符串
/// @param cents 分
+ (NSString *)changeDiscountFromCents:(NSInteger)cents {
    NSInteger beforeDot = cents / 10;
    NSInteger afterDot = cents % 10;
    if (afterDot == 0) {
        return [NSString stringWithFormat:@"%@",@(beforeDot)];
    }
    else {
        return [NSString stringWithFormat:@"%@.%@",@(beforeDot),@(afterDot)];
    }
}

/// 元转成分
- (NSInteger)changeToCents {
    NSString *afterValue = [NSString stringWithFormat:@"%0.2f",self.floatValue];
    
    NSArray *values = [afterValue componentsSeparatedByString:@"."];
    NSInteger cents = 0;
    if (values.count) {
        cents = [values[0] integerValue] * 100;
    }
    if (values.count > 1) {
        NSString *value = values[1];
        if (value.length == 1) {
            cents += value.integerValue * 10;
        }
        else {
            cents += [values[1] integerValue];
        }
    }
    return cents;
}

/// 拼接URL后面的参数
/// @param params 参数
- (NSString *)appendUrlParams:(NSDictionary *)params {
    NSArray * keys = [params allKeys];
    //拼接字符串
    NSString *url = [NSString stringWithString:self];
    for (NSString *key in keys) {
        if ([url containsString:@"?"]) {
           url = [url stringByAppendingFormat:@"&%@=%@",key,params[key]];
        }
        else {
            url = [url stringByAppendingFormat:@"?%@=%@",key,params[key]];
        }
    }
    return url;
}

/// url里面的中文处理
- (NSString *)encodeUrlChinese {
    return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}
@end
